package com.midea.ai.air.common;

import android.content.Context;
import android.graphics.Point;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * <br>类描述:公用的屏幕像素处理工具
 * <br>功能详细描述:初始化之后可以拿到一些常用的变量
 *
 * @author June Kwok
 * @date [2015-4-20]
 */
public class Constant {
    //maxxiang 头像裁剪用到
    public static final String SRC_IMAGE_PATH = "src_image_path";
    public static final String DST_IMAGE_PATH = "dst_image_path";
    public static final String SRC_IMAGE_URI = "src_image_uri";
    public static final String IS_SUCCESS = "is_success";


    public static final int REQUEST_CHOOSE_PORTRAIT = 1101;
    public static final int REQUEST_CROP_PORTRAIT = 1102;

    //后台协议部分用到的参数名称,统一定义在这里
    public static final String KEY_PAGE = "page";
    public static final String KEY_PAGE_SIZE = "pageSize";
    public static final String KEY_UID = "uid";
    public static final String KEY_TOKEN = "token";
    public static final String KEY_LID = "lid";
    public static final String KEY_TYPE = "type";
    public static final String KEY_CHANNELID = "channelId";
    public static final String KEY_RATE = "rate";
    public static final String KEY_USER_ID = "userId";
    public static final String KEY_NICK_NAME = "nickName";
    public static final String KEY_DEVICE_ID = "deviceId";
    public static final String KEY_APPVERSION = "appVersion";
    public static final String KEY_ENDTYPE = "endType";
    public static final String KEY_CUSTOMINFO = "customInfo";


    public static final String VIDEO_TYPE_0 = "直播";
    public static final String VIDEO_TYPE_1 = "点播";
    public static final String VIDEO_TYPE_2 = "链接";


    /**
     * 基础URl
     */
    public static final String BASE_URL = "http://101.200.125.76:8080/max-web-api";
    /**
     * sDateFormat
     */
    public static String sDateFormat = "yyyy年MM月dd日";
    /**
     * 默认屏幕的宽度
     */
    public static final int S_DEFAULT_WIDTH = 750;
    /**
     * 默认屏幕的高度
     */
    public static final int S_DEFAULT_HEIGHT = 1334;
    /**
     * 状态栏的高度
     */
    public static int sStatusBarHeight = 0;
    /**
     * 任务栏的高度
     */
    public static int sNavBarHeight = 0;
    /**
     * 屏幕宽度 单位 像素
     */
    public static int sRealWidth = S_DEFAULT_WIDTH;
    /**
     * 屏幕高度 单位 像素
     */
    public static int sRealHeight = S_DEFAULT_HEIGHT;

    /**
     * 宽度屏幕比例
     */
    public static float sScaleX = 1.0f;
    /**
     * 高度屏幕比例
     */
    public static float sScaleY = 1.0f;

    /**
     * 时间字体
     */
    public static Typeface sTypefaceTime;
    /**
     * 普通公共字体
     */
    public static Typeface sTypeface;
    /**
     * 数字字体
     */
    public static Typeface sTypefaceNum;
    /**
     * App是否全屏
     */
    public static boolean mIsFullScreen = false;

    /**
     * 获取视频类型
     *
     * @param s
     * @return
     */
    public static String getType(String s) {
        if ("0".equals(s)) {
            return VIDEO_TYPE_0;
        } else if ("1".equals(s)) {
            return VIDEO_TYPE_1;
        } else if ("2".equals(s)) {
            return VIDEO_TYPE_2;
        } else {
            return "VIDEO_TYPE_0";
        }
    }

    /**
     * 获取实际像素值根据宽度比例
     *
     * @param n
     * @return
     */
    public static float toPixf(int n) {
        return n * sRealWidth / S_DEFAULT_WIDTH;
    }

    /**
     * 获取实际像素值根据宽度比例
     *
     * @param n
     * @return
     */
    public static int toPix(int n) {
        return n * sRealWidth / S_DEFAULT_WIDTH;
    }

    /**
     * 获取实际像素值根据宽度比例
     *
     * @param n
     * @return
     */
    public static int toPix(boolean b, int n) {
        return b ? n * sRealWidth / S_DEFAULT_WIDTH : toPixByHeight(n);
    }

    /**
     * 获取实际像素值根据长度比例
     *
     * @param n
     * @return
     */
    public static int toPixByHeight(int n) {
        return n * sRealHeight / S_DEFAULT_HEIGHT;
    }

    /**
     * 获取实际像素值根据长度比例
     *
     * @param n
     * @return
     */
    public static float toPixByHeightf(int n) {
        return n * sRealHeight / S_DEFAULT_HEIGHT;
    }

    /**
     * <br>功能简述:初始化屏幕信息
     * <br>功能详细描述:
     * <br>注意:
     *
     * @param c
     */
    public static void initMetrics(Context ctx) {
        DisplayMetrics mDm = ctx.getResources().getDisplayMetrics();
        sRealWidth = mDm.widthPixels;
        sRealHeight = mDm.heightPixels;
        getStatusBarHeight(ctx);
        getNavBarHeight(ctx);
        sRealHeight = sRealHeight - sStatusBarHeight;
        if (mIsFullScreen) {
        } else {

        }
    }

    /**
     * <br>功能简述:初始化状态栏的高度
     * <br>功能详细描述:
     * <br>注意:
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context ctx) {
        if (sStatusBarHeight > 0) {
            return sStatusBarHeight;
        }
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            sStatusBarHeight = ctx.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return sStatusBarHeight;
    }

    /**
     * 初始化状透明任务栏的高度
     *
     * @param context
     */
    public static int getNavBarHeight(Context ctx) {
        if (sNavBarHeight > 0) {
            return sNavBarHeight;
        }
        if (ctx != null) {
            WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Class<?> sClass = null;
            try {
                if (sClass == null) {
                    sClass = Class.forName("android.view.Display");
                }
                Point realSize = new Point();
                Method method = sClass.getMethod("getRealSize", Point.class);
                method.invoke(display, realSize);
                sNavBarHeight = realSize.y - sRealHeight;
            } catch (Exception e) {
                sNavBarHeight = 0;
            }
        }
        return sNavBarHeight;
    }

    /**
     * 获取ActionBar距离上面的距离
     *
     * @return
     */
    public static int getTopMargin() {
        return mIsFullScreen ? sStatusBarHeight : 0;
    }

    /**
     * 数据初始化
     *
     * @param mCtx
     * @param bundle
     * @param sIsHideMxNarbar
     */
    public static void initData(Context mCtx, boolean sIsFull) {
        mIsFullScreen = sIsFull;
        initMetrics(mCtx);
    }
}
